/*=====================================================================
  This file is part of a Microsoft SQL Server Shared Source Application.
  Copyright (C) Microsoft Corporation.  All rights reserved.
 
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
======================================================= */

using System;
using System.Xml;
using System.Xml.Serialization;

namespace Microsoft.SqlServer.BISystems.Tool
{
    /// <summary>
    /// This class defines Client File information, e.g. Name, number of client files, etc. 
    /// </summary>
    [Serializable]
    public class ClientFile
    {
        private string m_name;

        [XmlAttribute(DataType = "string", AttributeName = "Name")]
        public string Name
        {
            get
            {
                return m_name;
            }
            set
            {
                try
                {
                    if (value == "")
                        throw new Exception("Client file name is empty.");

                    m_name = value;
                }
                catch (Exception e)
                {
                    throw new Exception("Error when deserializing tag \"ClientFile\", attribute \"Name\", with value: " + value, e);
                }
            }
        }

        private int m_startingNumber;

        [XmlAttribute(DataType = "string", AttributeName = "StartingNumber")]
        public string StartingNumberTag
        {
            get
            {
                return m_startingNumber.ToString();
            }
            set
            {
                try
                {
                    m_startingNumber = Int32.Parse(value);
                }
                catch (Exception e)
                {
                    throw new Exception("Error when deserializing tag \"ClientFile\", attribute \"StartingNumber\", with value: " + value, e);
                }
            }
        }

        [XmlIgnore]
        public int StartingNumber
        {
            get
            {
                return m_startingNumber;
            }
        }

        private int m_numOfFiles;

        [XmlAttribute(DataType = "string", AttributeName = "NumOfFiles")]
        public string NumOfFiles_Tag
        {
            get
            {
                return m_numOfFiles.ToString();
            }
            set
            {
                try
                {
                    m_numOfFiles = Int32.Parse(value);
                }
                catch (Exception e)
                {
                    throw new Exception("Error when deserializing tag \"ClientFile\", attribute \"NumOfFiles\", with value: " + value, e);
                }
            }
        }

        [XmlIgnore]
        public int NumOfFiles
        {
            get
            {
                return m_numOfFiles;
            }
        }

        private int m_DuplicateRetryCount;

        [XmlAttribute(DataType = "string", AttributeName = "DuplicateRetryCount")]
        public string DuplicateRetryCount_Tag
        {
            get
            {
                return m_DuplicateRetryCount.ToString();
            }
            set
            {
                try
                {
                    m_DuplicateRetryCount = Int32.Parse(value);
                }
                catch (Exception e)
                {
                    throw new Exception("Error when deserializing tag \"ClientFile\", attribute \"DuplicateRetryCount\", with value: " + value, e);
                }
            }
        }

        [XmlIgnore]
        public int DuplicateRetryCount
        {
            get
            {
                return m_DuplicateRetryCount;
            }
        }

        /// <summary>
        /// Get the complete list of client file names.
        /// </summary>
        /// <returns></returns>
        public string[] GetNames()
        {
            if (m_name == null)
                throw new Exception("Attribute \"Name\" in tag \"ClientFile\" is not specified");

            string[] fileNames = new string[m_numOfFiles];
            int fileId = m_startingNumber;

            for (int i = 0; i < m_numOfFiles; i++)
            {
                int id = m_name.LastIndexOf('.');

                fileNames[i] = m_name.Substring(0, id) + fileId.ToString() + m_name.Substring(id);

                fileId = fileId + 1;
            }

            return fileNames;
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public ClientFile()
        {
        }
    }
}
